//
//  RiskAppraisalView.m
//  SZBank
//
//  Created by rimi on 16/7/14.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "RiskAppraisalView.h"
#import "LYBtnLabel.h"
#import "BFKit.h"
#import "BJRiskShowView.h"
@interface RiskAppraisalView()<LYBtnLabelDelegate>
{
    CGFloat scrWidth;/**< 滚动视图宽度 */
    CGFloat scrHeight;/**< 滚动视图高度 */
    NSMutableDictionary *dictionary_result;/**< 得分字典 */
    NSMutableDictionary *dictionary_value;/**< 判断得分字典 */
}

@property (nonatomic)UILabel *titleLabel;/**< 标题 */
@property (nonatomic)UILabel *descripalLabel;/**< 描述 */
@property (nonatomic)UIButton *cheachButton;/**< 查看分数 */

@end
@implementation RiskAppraisalView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        dictionary_result = [NSMutableDictionary dictionary];
        dictionary_value = [NSMutableDictionary dictionary];
        scrWidth = self.scrollView.frame.size.width;
        scrHeight = self.scrollView.frame.size.width;
        
        self.scrollView.contentSize = CGSizeMake(scrWidth, scrHeight* 3.3);
        //添加视图
        [self.scrollView addSubview:self.titleLabel];
        [self.scrollView addSubview:self.descripalLabel];
        [self.scrollView addSubview:self.cheachButton];
        
        
        //网络访问
        [NetWorkingDataManager getProtocolListWithSuccess:^(id sender) {
            [self creatTableWithArray:sender];
//            NSLog(@"%@",sender);
    
          
        
            for (NSDictionary *dict in sender) {
                NSMutableArray *array_optionValue = [NSMutableArray array];
                for (NSDictionary *dictionary in dict[@"attributeOptionList"]) {
                    [array_optionValue addObject:dictionary[@"optionvalue"]];
                }
                [dictionary_value setObject:array_optionValue forKey:dict[@"id"]];
            }
        
            
            
            
            
            
        } failure:^(id sender) {
            NSLog(@"%@",sender);
        }];
    }
    return self;
}
#pragma mark -- actions
- (void)creatTableWithArray:(NSArray *)dataArray{
    
    //存储问题的数组
    NSMutableArray *array_name = [NSMutableArray array];
    //存储选项的数组
    NSMutableArray *array_choices = [NSMutableArray array];
    //存储ID数组
    NSMutableArray *array_ID = [NSMutableArray array];
    for (NSDictionary *dict in dataArray) {
        if (!([dict[@"name"] isEqualToString:@"客户姓名"] || [dict[@"name"] isEqualToString:@"手机号码"] || [dict[@"name"] isEqualToString:@"证件号码"])) {
            //判断除了以上情况之外就加入数组
            [array_name addObject:dict[@"name"]];
            [array_ID addObject:dict[@"id"]];
            [array_choices addObject:dict[@"attributeOptionList"]];
        }
        
    }
    
    //循环创建选项数据整理
    
    NSMutableArray *array_optionname = [NSMutableArray array];/**< 选项名数组 */
    NSArray *array_optionTitle = @[@"A",@"B",@"C",@"D",@"E"];/**< 添加选项title */
    for (NSArray *array in array_choices) {
        
        NSMutableArray *array_op = [NSMutableArray array];
        for (int i = 0; i < array.count; i ++) {
            NSMutableDictionary *dict_op = [NSMutableDictionary dictionary];
            [dict_op setObject:array[i][@"optionname"] forKey:@"optionname"];
            [dict_op setObject:array_optionTitle[i] forKey:@"optionvalue"];
            [array_op addObject:dict_op];
        }
        [array_optionname addObject:array_op];
    }
   // 循环创建问题视图
    for (int i = 0; i < array_name.count; i ++) {
        NSString *string = [NSString stringWithFormat:@"%d、%@",(i + 1),array_name[i]];
        //问题label
        UILabel *questionLabel = [[UILabel alloc]init];
        questionLabel.text = string;
        questionLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
   
        if (i < 4  ) {
             questionLabel.frame = CGRectMake(scrWidth * 0.05, CGRectGetMaxY(self.descripalLabel.frame) + i * scrHeight * 0.24, scrWidth * 0.9, scrHeight * 0.06);
            LYBtnLabel *buttonlabel = [[LYBtnLabel alloc]initWithFrame:CGRectMake(scrWidth * 0.05, CGRectGetMaxY(questionLabel.frame), scrWidth * 1.3, scrHeight * 0.06 * 5) array:array_optionname[i] nameId:array_ID[i]];
          buttonlabel.delegate = self;
            [self.scrollView addSubview:buttonlabel];
                                    
        }else if (i > 3 && i < 7){
            questionLabel.frame = CGRectMake(scrWidth * 0.05, CGRectGetMaxY(self.descripalLabel.frame) + i * scrHeight * 0.288 - scrHeight * 0.18, scrWidth * 0.9, scrHeight * 0.06);
            LYBtnLabel *buttonlabel = [[LYBtnLabel alloc]initWithFrame:CGRectMake(scrWidth * 0.06, CGRectGetMaxY(questionLabel.frame), scrWidth , scrHeight * 0.06 * 5) array:array_optionname[i] nameId:array_ID[i]];
            
         buttonlabel.delegate = self;
            [self.scrollView addSubview:buttonlabel];

        }else{
            questionLabel.frame = CGRectMake(scrWidth * 0.05, CGRectGetMaxY(self.descripalLabel.frame) + i * scrHeight * 0.24 + scrHeight * 0.16, scrWidth * 0.9, scrHeight * 0.06);
            LYBtnLabel *buttonlabel = [[LYBtnLabel alloc]initWithFrame:CGRectMake(scrWidth * 0.06, CGRectGetMaxY(questionLabel.frame), scrWidth , scrHeight * 0.06 * 5) array:array_optionname[i] nameId:array_ID[i]];
            
            buttonlabel.delegate = self;
            [self.scrollView addSubview:buttonlabel];

        }
    
        
        
        

        [self.scrollView addSubview:questionLabel];
    }
    

    
    
    
    
    
    
}


/**
 *  查看得分
 */
- (void)action_checkButton:(UIButton *)sender{
    [dictionary_result removeObjectForKey:@"string"];
    [dictionary_result removeObjectForKey:@"nameId"];
    if (dictionary_result.count < 11) {
        
        [LocalDataManager addAlertControllerWithControllerTitle:@"温馨提示" controllerMessage:@"请完成所有调查问题" actionSureTitle:@"确定" actionCancelTile:nil actionSureBlock:nil actionCancelBlock:nil style:0];
    }else{
        
        
        NSMutableArray *array_final = [NSMutableArray array];
        [dictionary_result enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            if (![key isEqualToString:@"402880c4401ee01301401eee34f60001"]) {
                if ([obj isEqualToString:@"A"]) {
                    [array_final addObject:dictionary_value[key][0]];
                }else if ([obj isEqualToString:@"B"]){
                    [array_final addObject:dictionary_value[key][1]];
                }else if ([obj isEqualToString:@"C"]){
                    [array_final addObject:dictionary_value[key][2]];
                }else if ([obj isEqualToString:@"D"]){
                    [array_final addObject:dictionary_value[key][3]];
                }else if ([obj isEqualToString:@"E"]){
                    [array_final addObject:dictionary_value[key][4]];
                }
            }
            
        }];
        
        //弹框提示
        NSInteger sum = 0;
        
        for (NSString *string in array_final) {
            sum += (long)[string integerValue];
        }
       
        
        
        //网络数据回传
        [NetWorkingDataManager getProtocolriskQuestionnaireTypeWithParam:nil success:^(id sender) {
           
            NSMutableArray *dataArray = [NSMutableArray array];
            [dataArray addObject:[NSString stringWithFormat:@"%ld",sum]];
            NSArray *array = sender[@"datalist"];
            for (NSDictionary *dic in array) {
                NSInteger low = [dic[@"SCORELOWER"] integerValue];
                NSInteger up = [dic[@"SCOREUP"] integerValue];
                NSString *low_up = [NSString stringWithFormat:@"%ld-%ld",low,up];
                if (sum >= low && sum <= up) {
                    [dataArray addObject:dic[@"NAME"]];
                    [dataArray addObject:low_up];
                    [dataArray addObject:dic[@"PROPOSE"]];
                }
            }
            
            [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            
            BJRiskShowView *showRisk = [[BJRiskShowView alloc]initWithFrame:CGRectMake(0, 0, scrWidth, scrHeight) withDataArray:dataArray];
            [self addSubview:showRisk];
           
        } failure:^(id sender) {
            NSLog(@"%@",sender);
        }];
        
        

    }

}
#pragma mark -- LYBtnLabelDelegate
- (void)btnLabel:(LYBtnLabel *)btnLabel inputDictionary:(NSDictionary *)inputDictionary{
    [inputDictionary enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
      [dictionary_result setObject:obj forKey:key];
    
    }];
  
}

#pragma mark -- getters

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, scrWidth, scrHeight * 0.1)];
        
        _titleLabel.text = @"个人客户投资风险承受能力评估问卷";
        _titleLabel.font = [UIFont systemFontOfSize:30.0];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    return _titleLabel;
}


- (UILabel *)descripalLabel{
    if (!_descripalLabel) {
        
        
        
        _descripalLabel = [[UILabel alloc]init];
        NSString *string = @"第一次与我行签约时需要填写本问卷，本次测试结果有效期为1年。一下11个问题将根据您的财务状况、投资经验、投资风格、风险偏好和风险承受能力等对你进行评估，我们将根据结果为您更好的配置资产。请您认真作答，感谢您的配合！（每个问题请选择唯一项，不可多选）" ;
        CGSize size = [string boundingRectWithSize:CGSizeMake(scrWidth * 0.8, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:FONT_SIZE]} context:nil].size ;
        
        [_descripalLabel sizeToFit];
        _descripalLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
        _descripalLabel.text = string;
        _descripalLabel.frame = CGRectMake(scrWidth* 0.05, scrWidth * 0.14, scrWidth * 0.9, size.height);
        _descripalLabel.numberOfLines = 0;
        
        
        
    }
    return _descripalLabel;
}

- (UIButton *)cheachButton{
    if (!_cheachButton) {
        _cheachButton = [[UIButton alloc]initWithFrame:CGRectMake(scrWidth* 0.2, scrHeight * 3.2, scrWidth * 0.15, scrHeight * 0.04)];
        [_cheachButton setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"理财_查看得分"] forState:UIControlStateNormal];
        [_cheachButton addTarget:self action:@selector(action_checkButton:) forControlEvents:UIControlEventTouchUpInside];
  
    }
    return _cheachButton;
}
@end
